package com.block.datapush.service.filter;

import cn.hutool.core.util.StrUtil;
import com.block.datapush.model.param.FilterHandlerParam;
import com.block.datapush.model.result.FilterHandlerResult;
import com.block.datapush.service.IDataFilterItemService;
import com.block.db.entity.MemberSourceInfo;
import org.springframework.stereotype.Service;

import java.time.LocalTime;

@Service
public class FilterTimeHandlerService implements IDataFilterItemService {

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberSourceInfo memberSourceInfo) {

        if (StrUtil.hasEmpty(handlerParam.getStartTime(), handlerParam.getEndTime())){
            return FilterHandlerResult.success();
        }

        LocalTime nowTime = LocalTime.now();
        LocalTime startTime = LocalTime.parse(handlerParam.getStartTime());
        LocalTime endTime = LocalTime.parse(handlerParam.getEndTime());

        if (startTime.isBefore(endTime)){
            if (nowTime.isAfter(startTime) && nowTime.isBefore(endTime)){
                return FilterHandlerResult.success();
            }
        }else {
            if (nowTime.isAfter(startTime) || nowTime.isBefore(endTime)){
                return FilterHandlerResult.success();
            }
        }

        String message = StrUtil.format("【startTime】{}【endTime】{}【nowTime】{}", startTime, endTime, nowTime);
        return FilterHandlerResult.error("TIME_ERROR", message);
    }

}
